#ifndef __MY_UTIL_HPP__
#define __MY_UTIL_HPP__

#include <iostream>
#include <string>
#include <ctime>
#include <cstring>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

namespace logs
{
    namespace util
    {
        /*时间操作工具类*/
        class Date
        {
        public:
            /*获取当前系统时间戳*/
            static size_t now()
            {
                return (size_t)time(nullptr);
            }
        };

        /*文件操作工具类*/
        class File
        {
        public:
            /*判断文件是否存在*/
            static bool exists(const std::string& filename)
            {
                struct stat st;
                bzero(&st,sizeof(st));
                if(stat(filename.c_str(),&st)<0) return false;
                return true;
            }

            /*获取文件所在目录*/
            static std::string path(const std::string& pathname)
            {
                //find_last_of是给定字符串中匹配复合的字符
                //find是给定字符串匹配的是字符串
                size_t pos=pathname.find_last_of("/\\");
                if(pos==std::string::npos) return ".";
                return pathname.substr(0,pos+1);
            }

            /*创建目录*/
            static void createDirectory(const std::string& pathname)
            {
                //"./abc/dce/"
                size_t index=0;
                size_t pos=0;
                while(index<pathname.size())
                {
                    pos=pathname.find_first_of("/\\",index);
                    if(pos==std::string::npos)
                    {
                        mkdir(pathname.c_str(),0777);
                        return;
                    }

                    std::string parentPath=pathname.substr(0,pos+1);
                    if(exists(parentPath)==true)
                    {
                        index=pos+1;
                        continue;
                    }
                    mkdir(parentPath.c_str(),0777);
                    index=pos+1;
                }
                return;
            }
        };
    }
}


#endif